﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WF1.lab03
{
    public partial class frmWordpad : Form
    {
        bool IsSave = true;
        String fileName = "";
        public frmWordpad()
        {
            InitializeComponent();
        }

        private void frmWordpad_Load(object sender, EventArgs e)
        {          
            cbbSize.SelectedIndex = 2;
            cbbFontName.SelectedIndex = 0;
            IsSave = true;
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void mnuNew_Click(object sender, EventArgs e)
        {
            rtbContent.Text = "";
            IsSave = true;
        }

        private void mnuOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Multiselect = false;
            dialog.Filter = "Rich text box format (*rtf)|*.rtf|All file|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                fileName = dialog.FileName;
                rtbContent.LoadFile(fileName);
                IsSave = true;
            }
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            saveAs();
        }

        private void saveAs()
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "Rich text box format (*rtf)|*.rtf|All file|*.*";
            if (fileName != "")
            {
                rtbContent.SaveFile(fileName);
            }
            else if (dialog.ShowDialog() == DialogResult.OK)
            {
                fileName = dialog.FileName;
                rtbContent.SaveFile(fileName);
                IsSave = true;
            }
        }

        private void cCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(rtbContent.SelectedText);
        }

        private void cCut_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(rtbContent.SelectedText);
            rtbContent.Text = "";
            IsSave = false;
        }

        private void cPaste_Click(object sender, EventArgs e)
        {
            rtbContent.Paste();
            IsSave = false;
        }

        private void cInsertImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Multiselect = false;
            dialog.Filter = "Image format (*jpg)|*.jpg";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Clipboard.SetImage(Image.FromFile(dialog.FileName));
                rtbContent.Paste();
                IsSave = false;
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            rtbContent.SelectionAlignment = HorizontalAlignment.Left;
            IsSave = false;
        }

        private void btnCenter_Click(object sender, EventArgs e)
        {
            rtbContent.SelectionAlignment = HorizontalAlignment.Center;
            IsSave = false;
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            rtbContent.SelectionAlignment = HorizontalAlignment.Right;
            IsSave = false;
        }       

        private void btnBgColor_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                rtbContent.SelectionBackColor = dialog.Color;
                IsSave = false;
            }
        }

        private void btnFontColor_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                rtbContent.SelectionColor = dialog.Color;
                IsSave = false;
            }
        }

        private void cFontFormat_Click(object sender, EventArgs e)
        {
            FontDialog dialog = new FontDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                rtbContent.SelectionFont = dialog.Font;
                IsSave = false;
            }
        }

        private void cbbFontName_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            rtbContent.SelectionFont = new Font(cbbFontName.Text,float.Parse(cbbSize.Text));
            IsSave = false;
        }

        private void cbbSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            rtbContent.SelectionFont = new Font(cbbFontName.Text, float.Parse(cbbSize.Text));
            IsSave = false;
        }

        private void cbbSize_TextUpdate(object sender, EventArgs e)
        {
            try
            {
                rtbContent.SelectionFont = new Font(cbbFontName.Text, float.Parse(cbbSize.Text));
                IsSave = false;
            }
            catch (Exception)
            {
                MessageBox.Show("You can't entry character!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                cbbSize.Text = "14";
                IsSave = false;
                cbbSize.Focus();
            }
            
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            Font f = rtbContent.SelectionFont;
            FontStyle fs = rtbContent.SelectionFont.Style;
            

            if (f.Bold == true)
            {
                fs -= FontStyle.Bold;
                btnBold.CheckState = CheckState.Unchecked;
            }
            else
            {
                fs |= FontStyle.Bold;
                btnBold.CheckState = CheckState.Checked;
            }

            rtbContent.SelectionFont = new Font(cbbFontName.Text,float.Parse(cbbSize.Text),fs);
            IsSave = false;
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            Font f = rtbContent.SelectionFont;
            FontStyle fs = rtbContent.SelectionFont.Style;


            if (f.Italic == true)
            {
                fs -= FontStyle.Italic;
                btnItalic.CheckState = CheckState.Unchecked;
            }
            else
            {
                fs |= FontStyle.Italic;
                btnItalic.CheckState = CheckState.Checked;
            }

            rtbContent.SelectionFont = new Font(cbbFontName.Text, float.Parse(cbbSize.Text), fs);
            IsSave = false;
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            Font f = rtbContent.SelectionFont;
            FontStyle fs = rtbContent.SelectionFont.Style;


            if (f.Underline == true)
            {
                fs -= FontStyle.Underline;
                btnUnderline.CheckState = CheckState.Unchecked;
            }
            else
            {
                fs |= FontStyle.Underline;
                btnUnderline.CheckState = CheckState.Checked;
            }

            rtbContent.SelectionFont = new Font(cbbFontName.Text, float.Parse(cbbSize.Text), fs);
            IsSave = false;
        }

        private void rtbContent_TextChanged(object sender, EventArgs e)
        {
            IsSave = false;
        }

        private void frmWordpad_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsSave==false)
            {
                DialogResult rs = MessageBox.Show("You want save document?","Warring",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
                if (rs == DialogResult.Yes)
                {
                    saveAs();
                }
                else if (rs == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        } 

    }
}
